clear
clear matrix
scalar drop _all
set more off

* ------------------------------------------------------------------------------
* set paths
global root "[include file path here]"
global input "$root/Input"
global output "$root/Output"
global intermediate "$root/Intermediate"
global figures "$root/Figures"


set more off
*_______________________________________________________________________
use "$input/eventStudyPanel_raw.dta", clear
keep if conversions == 0 | (conversions == 1 & wg == "SF")

scalar drop _all

preserve // all observations
	duplicates drop pid, force
	scalar scalar_parcels_full = _N
restore
	
preserve // non-participants
	keep if enroll2 == 0
	duplicates drop pid, force
	scalar scalar_parcels_non = _N
restore
	
preserve // participants pre-enroll
	keep if enroll2 == 1
	duplicates drop pid, force
	scalar scalar_parcels_pre = _N
restore
	
preserve // participants post-enroll
	keep if enroll2 == 2
	duplicates drop pid, force
	scalar scalar_parcels_post = _N
restore

egen mean_wuse = mean(wuse)
egen mean_lotsize = mean(lotsize)
egen obs_wuse = count(wuse)
egen obs_lotsize = count(lotsize)
gen parcels_full = scalar_parcels_full
gen parcels_non = scalar_parcels_non
gen parcels_pre = scalar_parcels_pre
gen parcels_post = scalar_parcels_post

keep wuse lotsize enroll2 mean_wuse mean_lotsize obs_wuse obs_lotsize parcels_*
collapse ///
	(mean) wuse ///
	(count) obs = wuse ///
	(mean) lotsize ///
	(count) obs_lotsize = lotsize ///
	(first) full_mean_wuse = mean_wuse ///
	(first) full_mean_lotsize = mean_lotsize ///
	(first) full_obs_wuse = obs_wuse ///
	(first) full_obs_lotsize = obs_lotsize ///
	(first) parcels_full ///
	(first) parcels_non ///
	(first) parcels_pre ///
	(first) parcels_post, by(enroll2) fast

la da "table1 sum stats for raw water panel"
save "$intermediate/tab1sumstat_eventStudyPanel_raw.dta", replace
	
*_______________________________________________________________________
use "$input/eventStudyPanel_matched.dta", clear

scalar drop _all

preserve // all observations
	duplicates drop pid, force
	scalar scalar_parcels_full = _N
restore
	
preserve // non-participants
	keep if enroll2 == 0
	duplicates drop pid, force
	scalar scalar_parcels_non = _N
restore
	
preserve // participants pre-enroll
	keep if enroll2 == 1
	duplicates drop pid, force
	scalar scalar_parcels_pre = _N
restore
	
preserve // participants post-enroll
	keep if enroll2 == 2
	duplicates drop pid, force
	scalar scalar_parcels_post = _N
restore

egen mean_wuse = mean(wuse)
egen mean_lotsize = mean(lotsize)
egen obs_wuse = count(wuse)
egen obs_lotsize = count(lotsize)
gen parcels_full = scalar_parcels_full
gen parcels_non = scalar_parcels_non
gen parcels_pre = scalar_parcels_pre
gen parcels_post = scalar_parcels_post

keep wuse lotsize enroll2 mean_wuse mean_lotsize obs_wuse obs_lotsize parcels_*
collapse ///
	(mean) wuse ///
	(count) obs = wuse ///
	(mean) lotsize ///
	(count) obs_lotsize = lotsize ///
	(first) full_mean_wuse = mean_wuse ///
	(first) full_mean_lotsize = mean_lotsize ///
	(first) full_obs_wuse = obs_wuse ///
	(first) full_obs_lotsize = obs_lotsize ///
	(first) parcels_full ///
	(first) parcels_non ///
	(first) parcels_pre ///
	(first) parcels_post, by(enroll2) fast

la da "table1 sum stats for matched water panel"
save "$intermediate/tab1sumstat_eventStudyPanel_matched.dta", replace

*_______________________________________________________________________
/* This section of code needs to be run twice

To generate Table 1:
set ctiop, paneltype, and panelti to Full, full, and raw

To generate Table 2:
set ctiop, paneltype, and panelti to Matched, matched, and matched */

local ctiop "Matched" // Full or Matched
local paneltype "matched" // full or matched
local panelti "matched" // raw or matched

display "table title: `ctiop', type: `paneltype', file title: `panelti'"

use "$intermediate/tab1sumstat_eventStudyPanel_`panelti'.dta", clear
mkmat _all, matrix(data)

mat T = J(4,4,.)
	mat T[1,1] = data[1,8]
	mat T[1,2] = data[1,3]
	mat T[1,3] = data[2,3]
	mat T[1,4] = data[3,3]
	mat T[2,1] = data[1,10]
	mat T[2,2] = data[1,11]
	mat T[2,3] = data[1,12]
	mat T[2,4] = data[1,13]
	mat T[3,1] = data[1,6]
	mat T[3,2] = data[1,2]
	mat T[3,3] = data[2,2]
	mat T[3,4] = data[3,2]
	mat T[4,1] = data[1,7]*1000
	mat T[4,2] = data[1,4]*1000
	mat T[4,3] = data[2,4]*1000
	mat T[4,4] = data[3,4]*1000
	
local rnames "Monthly water use obs. (\(N\))" "Number of parcels" "Mean water use (kgal/mo)" "Mean lot size (sq-ft)"
local tbloption1b "tex fragment replace coljust(rl) hlines(101{0}1) spacebef(101{0})"
local tbloption2 "sdec(0\0\1\0)"

* output matrix to table
mat rownames T = "`rnames'"
frmttable using "$output/sumstats_table_`paneltype'.tex", statmat(T) ///
`tbloption1b' `tbloption2' multicol(1,4,2) ctitle("","","","Participant","" \ ///
"{\bf `ctiop' panel}","All parcels","Non-participant","\emph{Pre-enrollment}","\emph{Post-enrollment}")